package com.shaozi.nc.quartz.attendance;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;

import com.shaozi.common.schedule.DyncQuartzManage;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.consts.SCConsts.QuartzKey;
import com.shaozi.nc.data.hibVO.AttendanceVO;
import com.shaozi.nc.hibservice.AttendanceService;
import com.shaozi.nc.quartz.base.BaseRuleJobExecute;
import com.shaozi.nc.quartz.base.Consts;
import com.shaozi.utils.date.DateHelper;

/**
 * 零晨启动调度，获取异常考勤的用户
 * 
 * @author brenx
 *
 */
public class ComplainEndDateRuleJobExecute extends BaseRuleJobExecute {

	@Autowired
	private AttendanceService attendanceService;
	@Autowired
	private DyncQuartzManage dyncQuartzMgr;
	private Logger logger = Logger.getLogger(ComplainEndDateRuleJobExecute.class);

	public ComplainEndDateRuleJobExecute() {

	}

	@Override
	protected void handleCompany(String companyId) {
		try {
			List<AttendanceVO> vos = attendanceService.getAttendanceUserByGrouop(companyId);
			if (vos.isEmpty()) {
				logger.info("company=" + companyId + " load AttendanceVO null");
				return;
			}
			int cnt = 0;
			for (AttendanceVO vo : vos) {
				try {
					boolean ok = addComplainEndDateJob(companyId, vo);
					if (ok)
						cnt++;
				} catch (Exception e) {
					logger.error(SzException.getStackMessage(e));
				}
			}
			logger.info("company=" + companyId + ";attendance complain:" + cnt);
		} catch (SzException e) {
			logger.error(e.getStackMessage());
			return;
		}
	}

	private boolean addComplainEndDateJob(String companyId, AttendanceVO vo) throws SchedulerException {
		if ((vo.getRuleHandleTime() == null) || (vo.getRuleHandleTime() == 0l))
			return false;
		Calendar cal = DateHelper.getCalendar();
		cal.setTimeInMillis(vo.getRuleHandleTime());

		Calendar startCal = DateHelper.getCalendar();
		startCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
		startCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
		startCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
		long startAt = startCal.getTimeInMillis();
		if (startAt < System.currentTimeMillis()) {
			return false;
		}
		String jobName = String.format("%s.%s.%d", companyId, "ComplainEndDate", vo.getId());
		String triggerName = String.format("trigger.%s", jobName);
		String groupKey = QuartzKey.ComplainEndDateGroupKey;
		Map<String, String> dataMap = new HashMap<>();
		dataMap.put("companyId", companyId);
		dataMap.put("id", String.valueOf(vo.getId()));
		dataMap.put("uid", String.valueOf(vo.getUid()));
		dataMap.put(Consts.DataKey_StartAt, String.valueOf(startAt));
		if (dyncQuartzMgr.existTrigger(groupKey, triggerName)) {
			dyncQuartzMgr.removeJob(groupKey, jobName, groupKey, triggerName);
		}
		dyncQuartzMgr.addJobWithOnce(groupKey, jobName, ComplainEndDateJob.class, groupKey, triggerName, startAt,
				dataMap);
		return true;
	}

}
